from typing import List, Tuple, Any

from PySide6.QtGui import Qt
from PySide6.QtWidgets import QTableWidget, QAbstractItemView

from common.equipment.reference import EQUIPMENT_SLOTS

DX = 1

# 窗口尺寸
SIZE_WIDTH = 1000
SIZE_HEIGHT = 618
# 对话框尺寸
DIALOG_WIDTH = SIZE_WIDTH // 2
DIALOG_HEIGHT = SIZE_HEIGHT // 2

# 格子宽度
LW = 50

# 每行或每列的格子数
LN = 10

# 主地图线长
LINE_LENGTH = LW * LN

MAP_TITLE_HEIGHT = 30
R_SIDE_WIDTH = SIZE_WIDTH - LINE_LENGTH - DX * 4
R_SIDE_LEFT = LINE_LENGTH + DX * 4
QUICK_BAR_TOP = MAP_TITLE_HEIGHT + DX
QUICK_BAR_HEIGHT = 30
E_TOP = QUICK_BAR_TOP + QUICK_BAR_HEIGHT + DX
E_LINE_HEIGHT = 30
E_HEIGHT = E_LINE_HEIGHT * len(EQUIPMENT_SLOTS) + DX * 2
PLAYER_PANEL_TOP = E_TOP + E_HEIGHT + DX
PLAYER_PANEL_HEIGHT = SIZE_HEIGHT - E_TOP - E_HEIGHT - DX * 4


# 行列转坐标
def rc_to_xy(pos: (int, int)) -> (int, int):
    return DX + pos[1] * LW, DX + pos[0] * LW


# 坐标转行列
def xy_to_rc(xy: (int, int)) -> (int, int):
    r = (xy[1] - DX) // LW
    c = (xy[0] - DX) // LW
    return r, c


def html_table(rows: List[Tuple[str, Any]], title=None):
    root = f"<h1>{title}</h1>" if title is not None else ""

    h_table = ''
    for c1, c2 in rows:
        h_c1 = f'<td style="font-weight:bold;text-align:right;padding:5px;">{c1}</td>'
        if isinstance(c2, List):
            t = ''.join(f'<tr><td><a href="action:test">{c}</a></td></tr>' for c in c2)
            h_c2 = f'<td><table>{t}</table></td>'
        else:
            h_c2 = f'<td style="text-align:left;padding:5px;">{c2}</td>'

        h_table += f'<tr>{h_c1}{h_c2}</tr>'

    return root + f'<h2><table border="1">{h_table}</table></h2>'


def set_table_default(table: QTableWidget, left_width: int = None):
    table.setColumnCount(2)
    table.setFocusPolicy(Qt.FocusPolicy.NoFocus)
    table.verticalHeader().setVisible(False)
    table.horizontalHeader().setVisible(False)
    table.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
    table.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
    table.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)
    table.setContentsMargins(0, 0, 0, 0)

    if left_width is not None:
        table.setColumnWidth(0, left_width)
        table.setColumnWidth(1, table.width() - left_width)

    table.setStyleSheet("""
            QTableWidget::item {
                background-color: white; /* 单元格默认背景色 */
                selection-background-color: white; /* 选中时的背景色也设置为白色 */
                height: 30px;
            }
        """)


class Pos:
    __slots__ = ('row', 'col')

    def __init__(self, row, col):
        self.row = row
        self.col = col

    def to_xy(self):
        return DX + self.col * LW, DX + self.row * LW

    def __str__(self):
        return f'({self.row}, {self.col})'

    def __json__(self):
        return f'{self.row},{self.col}'

    def load(self, str_pos: str):
        row, col = str_pos.split(',')
        self.set_pos(int(row), int(col))

    @property
    def key(self):
        return self.row, self.col

    def set_pos(self, row, col):
        self.row = row
        self.col = col

    def __eq__(self, other):
        if isinstance(other, tuple) or isinstance(other, list):
            return len(other) == 2 and self.row == other[0] and self.col == other[1]

        return self.row == other.row and self.col == other.col
